package Bio::Rambam::Proteomics::Searches::OMSSA_search;
use Moose;
use namespace::autoclean;
with 'Bio::Rambam::Proteomics::Tools::ENGINE_search';
use Bio::Rambam::Proteomics qw(%ENZYMES %MODS $TEMPLATES);
use Data::Dumper;
use File::Basename;
use IPC::System::Simple qw(capture);
use Try::Tiny;
  
has 'search'	=>	(is=>'rw',	isa=>'Bio::Rambam::Proteomics::Search',	default=>sub{{}}, trigger =>\&_start_omssa);

sub _start_omssa{
	my $self=shift;
	my $search=$self->search;
	###	Paths
	my $omssa_path=$search->engines->omssa;
	die "OMSSA path is not correct.\n You should use the complete path to the executable file.\n" unless -B$omssa_path;
	my $omssa_out_folder=$search->folder."/omssa";	#omly works for linux
	my $omssa_out_file = basename($search->input_file);
	$omssa_out_file =$omssa_out_folder.'/'.$omssa_out_file;
	$omssa_out_file=~s/\.mgf/\.xml/;
	mkdir $omssa_out_folder unless -d$omssa_out_folder;
	############################################################	OMSSA Arguments
	my $omssa_mods=$omssa_out_folder.'/'."mods.xml";
	my %mods=_generate_omssa_mods_file($search,$omssa_mods);
	die "OMSSA max missed cleavages are 3" if $search->miss_cleavages>3;
	my $command="$omssa_path -d ".$search->database;
	$command.=" -fm ".$search->input_file;		################################MIRA ESTOOOO
	$command.=" -mux ".$omssa_mods;
	$command.=" -mf ".(join ',',@{$mods{fixed_mods}}) if defined $mods{fixed_mods};
	$command.=" -mv ".(join ',',@{$mods{variable_mods}}) if defined $mods{variable_mods};
	########$command.=" -mf 3 -mv 1,173,174,175";
	$command.=" -zh ".$search->top_peptide_charge;
	$command.=" -zl 1";
#	if(($search->fragmentation eq 'ECD') or ($search->fragmentation eq 'ETD')){
#		$command.=" -i 2,4,5";
#	}
#	else{
#		$command.=" -i 1,4";
#	}
	if($search->pep_tol_units eq 'Da'){
		$command.=" -te ".$search->peptide_tol;
	}
	elsif($search->pep_tol_units eq 'ppm'){
		$command.=" -teppm -te ".$search->peptide_tol;
	}
	$command.=" -to ".$search->fragment_tol;
	$command.=" -v ".$search->miss_cleavages;
	$command.=" -e ".$ENZYMES{$search->enzyme}{OMSSA};
	$command.=" -nt ".$search->threads;
	$command.=" -op ".$omssa_out_file;
	#$command.=" 2>&1";
	open OUT,">/home/david/kk.txt";
	print OUT $command;
	
	########################################################################	End of arguments
	run_command($command,$self);
	$self->output_file($omssa_out_file);
}

sub _generate_omssa_mods_file{
	my ($search,$file)=@_;
	my $omssa_mods_template=$TEMPLATES."omssa_mods.xml";
	my $mods_used=1;
	my %mods;
	my %replacements;
	foreach(split /\,/,$search->fixed_mods){
		my ($mod,$res)=split /\@/;
		if($res eq 'Nt'){
			$replacements{"mod".$mods_used."type"}="modnp";
			$replacements{"mod".$mods_used."typenum"}=5;
			$replacements{"mod".$mods_used."res"}="";
		}
		elsif($res eq 'Ct'){
			$replacements{"mod".$mods_used."type"}="modcp";
			$replacements{"mod".$mods_used."typenum"}=7;
			$replacements{"mod".$mods_used."res"}="";
		}
		else{
			$replacements{"mod".$mods_used."type"}="modaa";
			$replacements{"mod".$mods_used."typenum"}=0;
			$replacements{"mod".$mods_used."res"}=$res;
		}
		$replacements{"mod".$mods_used."name"}=$MODS{$mod}{name};
		$replacements{"mod".$mods_used."mass"}=$MODS{$mod}{mass};
		push @{$mods{fixed_mods}},118+$mods_used;
		$mods_used++;
	}
	foreach(split /\,/,$search->variable_mods){
		my ($mod,$res)=split /\@/;
		if($res eq 'Nt'){
			$replacements{"mod".$mods_used."type"}="modnp";
			$replacements{"mod".$mods_used."typenum"}=5;
			$replacements{"mod".$mods_used."res"}="";
		}
		elsif($res eq 'Ct'){
			$replacements{"mod".$mods_used."type"}="modcp";
			$replacements{"mod".$mods_used."typenum"}=7;
			$replacements{"mod".$mods_used."res"}="";
		}
		else{
			$replacements{"mod".$mods_used."type"}="modaa";
			$replacements{"mod".$mods_used."typenum"}=0;
			$replacements{"mod".$mods_used."res"}=$res;
		}
		$replacements{"mod".$mods_used."name"}=$MODS{$mod}{name};
		$replacements{"mod".$mods_used."mass"}=$MODS{$mod}{mass};
		push @{$mods{variable_mods}},118+$mods_used;
		$mods_used++;
	}
	my $omssa_mods_text=template($omssa_mods_template,\%replacements);
	open my $output,">",$file;
	print $output $omssa_mods_text;
	return %mods;
	
}


__PACKAGE__->meta->make_immutable;